package com.guodi.map.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapBookMark;
import com.guodi.map.entity.MapThematicLog;
import com.guodi.map.service.IMapThematicLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @描述：专题图制作日志表
 * @作者: 陈宏成
 * @日期: 2021-04-26 20:56:00
 */
@RestController
@RequestMapping("/MapThematicLog")
@Api(value = "API - MapThematicLogController", tags = "专题图制作日志接口")
public class MapThematicLogController extends BaseController {

    @Resource
    private IMapThematicLogService iMapThematicLogService;

    /**
     * @描述：新增专题图制作日志
     * @作者: 陈宏成
     * @日期: 2021-04-26 20:56:00
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增专题图制作日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "mapType", value = "地图类型", required = true, dataType = "string", paramType = "query")
    })
    @Transactional(rollbackFor = Exception.class)
    public R save(@ApiIgnore MapThematicLog mapThematicLog){
        // 新增前的验证数据，填充数据
        R<MapBookMark> prepareBeforeSaveResult = this.prepareBeforeSave(mapThematicLog);
        if (prepareBeforeSaveResult != null){
            return prepareBeforeSaveResult;
        }
        boolean save = this.iMapThematicLogService.save(mapThematicLog);
        if (save){
            return R.data(mapThematicLog);
        }else{
            return R.fail(StatusCode.ERROR_CODE_40005,"新增失败");
        }
    }



    /**
     * @描述：删除专题图制作日志
     * @作者: 陈宏成
     * @日期: 2021-04-26 20:56:00
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除专题图制作日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键id，如果有多个，逗号隔开", required = true, dataType = "string", paramType = "query")
    })
    @Transactional(rollbackFor = Exception.class)
    public R delete(String ids){
        if (!StringUtils.hasText(ids)){
            return new R();
        }
        String[] split = ids.split(",");
        List<String> idList = Arrays.asList(split).stream().map(id -> {
            return id.trim();
        }).collect(Collectors.toList());
        // 查询
        LambdaQueryWrapper<MapThematicLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(MapThematicLog::getId,idList);
        queryWrapper.eq(MapThematicLog::getCreator,getUser().getUserId().toString());
        List<MapThematicLog> mapThematicLogs = this.iMapThematicLogService.list(queryWrapper);
        // 删除
        boolean remove = this.iMapThematicLogService.remove(queryWrapper);
        // 返回
        if (remove){
            return R.data(mapThematicLogs);
        }else{
            return R.fail(StatusCode.ERROR_CODE_40005,"删除失败");
        }
    }


    /**
     * @描述：分页获取专题图制作日志
     * @作者: 陈宏成
     * @日期: 2021-04-26 20:56:00
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页获取专题图制作日志")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "title", value = "标题",  dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "页码",  dataType = "integer", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页面大小",  dataType = "integer", paramType = "query"),
    })
    public R<Page<MapThematicLog>> page(@ApiIgnore Page page, String title){
        // 查询
        LambdaQueryWrapper<MapThematicLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(title), MapThematicLog::getTitle, title);
        queryWrapper.eq(MapThematicLog::getCreator,getUser().getUserId().toString());
        queryWrapper.orderByDesc(MapThematicLog::getCreateTime);
        Page page1 = this.iMapThematicLogService.page(page, queryWrapper);
        return R.data(page1);
    }



    /**
     * @描述：新增前的准备，包括验证数据和填充数据
     * @入参：mapThematicLog
     * @出参：如果返回null，则表示无任何问题，如果有返回，则说明有问题需要返回给前端
     * @作者: 陈宏成
     * @日期: 2021-04-26 20:56:00
     */
    private R<MapBookMark> prepareBeforeSave(MapThematicLog mapThematicLog){
        // 验证非空数据项
        if (!StringUtils.hasText(mapThematicLog.getTitle())){
            return R.fail(StatusCode.ERROR_CODE_40005,"标题不能为空");
        }
        if (!StringUtils.hasText(mapThematicLog.getId())){
            mapThematicLog.setId(UuidUtil.get32UUID());
        }
        if (Objects.equals("undefined", mapThematicLog.getMapType()) || Objects.equals("null", mapThematicLog.getMapType())){
            // 如果是 undefined ，直接算作null
            mapThematicLog.setMapType(null);
        }
        mapThematicLog.setCreator(getUser().getUserId().toString());
        mapThematicLog.setCreateTime(new Date());
        return null;
    }


}
